﻿using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Admin_Admin_ShopGroup : Admin, IRequiresSessionState
{
    protected int gid;
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(17))
        {
            base.WriteRightErrMsg();
        }
        this.gid = Fetch.GetID("ID");
        if (!base.IsPostBack)
        {
            if (this.gid > 0 || Fetch.Get("Action") == "add")
            {
                this.GridView1.Visible = false;
                if (Fetch.Get("Action") == "add")
                {
                    this.FormView1.DefaultMode = FormViewMode.Insert;
                }
                this.FormView1.DataSourceID = "ObjectDataSource2";
                this.FormView1.DataBind();
                this.FormView1.Visible = true;
                if (Fetch.Get("Action") != "add")
                {
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.Load(base.Server.MapPath("..") + "/App_Data/CloseDate.xml");
                    XmlNode xmlNode = xmlDocument.SelectSingleNode("/Items/s" + this.gid.ToString());
                    if (xmlNode != null)
                    {
                        ((TextBox)this.FormView1.FindControl("CloseDate")).Text = xmlNode.InnerText;
                    }
                }
            }
            else
            {
                this.GridView1.DataSourceID = "ObjectDataSource1";
            }
        }
    }
    protected void OnSubMit(object sender, ObjectDataSourceMethodEventArgs e)
    {
        string text = "";
        CheckBoxList checkBoxList = (CheckBoxList)this.FormView1.FindControl("GroupPower");
        foreach (ListItem listItem in checkBoxList.Items)
        {
            if (listItem.Selected)
            {
                text = text + listItem.Value + ",";
            }
        }
        if (text != "")
        {
            text += ",";
            text = text.Replace(",,", "");
        }
        e.InputParameters["Power"] = text;
        int num = 0;
        CheckBox checkBox = (CheckBox)this.FormView1.FindControl("chk_IsUseSecUrl");
        if (checkBox != null)
        {
            if (checkBox.Checked)
            {
                num = 1;
            }
        }
        e.InputParameters["IsUseSecUrl"] = num;
    }
    protected void ObjectDataSource2_OnUpdated(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue == null)
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/EditFail", e.Exception.Message.ToString()), "");
        }
        if (e.ReturnValue.ToString().ToLower() == "true")
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(base.Server.MapPath("..") + "/App_Data/CloseDate.xml");
            XmlNode xmlNode = xmlDocument.SelectSingleNode("/Items/s" + this.gid.ToString());
            if (xmlNode == null)
            {
                xmlNode = xmlDocument.CreateElement("s" + this.gid.ToString());
                xmlDocument.DocumentElement.AppendChild(xmlNode);
            }
            xmlNode.InnerText = ((TextBox)this.FormView1.FindControl("CloseDate")).Text;
            xmlDocument.Save(base.Server.MapPath("..") + "/App_Data/CloseDate.xml");
            this.WriteSuccessMsg("", Language.GetAdminMsg("Common/EditSuccess"), "Admin_ShopGroup.aspx");
        }
        else
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/EditFail", "0"), "");
        }
    }
    protected void ObjectDataSource2_OnInserted(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue == null)
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/AddFail", e.Exception.Message.ToString()), "");
        }
        if (e.ReturnValue.ToString().ToLower() == "true")
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(base.Server.MapPath("..") + "/App_Data/CloseDate.xml");
            XmlNode xmlNode = xmlDocument.SelectSingleNode("/Items/s" + this.gid.ToString());
            if (xmlNode == null)
            {
                xmlNode = xmlDocument.CreateElement("s" + this.gid.ToString());
                xmlDocument.DocumentElement.AppendChild(xmlNode);
            }
            xmlNode.InnerText = ((TextBox)this.FormView1.FindControl("CloseDate")).Text;
            xmlDocument.Save(base.Server.MapPath("..") + "/App_Data/CloseDate.xml");
            this.WriteSuccessMsg("", Language.GetAdminMsg("Common/AddSuccess"), "Admin_ShopGroup.aspx");
        }
        else
        {
            this.WriteErrMsg("", Language.GetAdminMsg("Common/AddFail", "0"), "");
        }
    }
    protected void FormView1_OnDataBound(object sender, EventArgs e)
    {
        if (this.FormView1.DefaultMode == FormViewMode.Edit)
        {
            ShopGroupBll shopGroupBll = new ShopGroupBll(this.conn);
            DataTable groupByID = shopGroupBll.GetGroupByID(this.gid);
            CheckBoxList checkBoxList = (CheckBoxList)this.FormView1.FindControl("GroupPower");
            string content = groupByID.Rows[0]["Power"].ToString();
            foreach (ListItem listItem in checkBoxList.Items)
            {
                listItem.Selected = shopGroupBll.CheckGroupPower(content, listItem.Value);
            }
            CheckBox checkBox = (CheckBox)this.FormView1.FindControl("chk_IsUseSecUrl");
            if (checkBox != null)
            {
                string text = groupByID.Rows[0]["IsUseSecUrl"].ToString();
                if (!string.IsNullOrEmpty(text))
                {
                    if (text.Trim() != "0")
                    {
                        checkBox.Checked = true;
                    }
                }
            }
        }
    }
    protected void DeleteGroup(object sender, CommandEventArgs e)
    {
        this.gid = Convert.ToInt32(e.CommandArgument);
        if (this.gid > 0)
        {
            ShopGroupBll shopGroupBll = new ShopGroupBll(this.conn);
            if (shopGroupBll.CountShopNum(this.gid) > 0)
            {
                this.WriteErrMsg("", Language.GetAdminMsg("ShopGroup/ShopNumErr"), "");
            }
            if (shopGroupBll.DeleteGroup(this.gid))
            {
                this.WriteSuccessMsg("", Language.GetAdminMsg("Common/DeleteSuccess"), "Admin_ShopGroup.aspx");
            }
        }
    }
}
